<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/27
 * Time: 9:33
 */

namespace app\admin\controller;
use     think\Image;
use     think\cache;
use     think\Exception;
use     think\cache\driver\Redis;
class UploadController  extends CommonController
{
    public $initModel           =   false;
    public $is_strictly         =   false;
    //临时访问的方法    需要添加的额外的方法 否则只能请求默认的方法  ,默认请求的办法
    public $tmp_method          =   ['run'=>'POST'];
    private $redisHandler       =   null;
    private $prefix             =   'image_upload_';
    private $second             =   1200;  //半个小时上传检测有的话那么就直接持久化
    public function _initialize()
    {
        //必须要执行父级的该方法否则会覆盖
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->redisHandler     =   new Redis();
    }

    /**
     * 上传图片方法  公共上传方法
     */
    public function run()
    {
        try{
            //改值作为当前的判断图片在不在的依据是否再次做上传的操作
            $upload_hash            =   $this->param['hash_val'];
            if(true===cache::has($this->prefix.$upload_hash)){
               throw new Exception('抱歉当前上传图片已经存在！',self::SUCCESS_CODE);
            }
            //获取当前的文件上传stream
           $upload_config           =    config('upload.default'); //这里可以自己加上我们的属性
           //当前上传检测
           if(true===$upload_config) {
               throw new Exception('抱歉图片上传设置错误',self::SUCCESS_CODE);
           }
           if(true===empty($upload_config['validate_field'])){
               throw new Exception('抱歉上传验证失败！',self::SUCCESS_CODE);
           }
           //判断当前是否和配置的字段相符;
           array_map(function($element){
            if(false===$this->request->has($element)) {
               throw new Exception('抱歉字段验证错误！',self::SUCCESS_CODE);
            }
           },$upload_config['validate_field']);
           //获得当前的图片
           $file                    =   $this->request->file($this->param['upload_name']);
           //验证当上传是否合法
           $result                  =   $file->validate($upload_config['upload_config']);
           //判断上传是否合法
           if(false===$result){
               throw new Exception('抱歉非法上传！');
           }
           //com_create_guid()  //生成一个guid 现在还没有linux的扩展有win版的扩展
           $info                    =   $file->rule($upload_config['rule'])->move($upload_config['path'],true,true);

           if(false===$info) {
               throw new Exception($file->getError(),self::SUCCESS_CODE);
           }
           //建议获得当前文件的md5中存储数据库以做持久化处理
           cache::set($this->prefix.$upload_hash,$upload_hash,$this->second);
          //得到MD5加密至
           $upload_hash_db          =   $info->md5(); //file类用了__call（$method,$args）;
           //现在是要做数据的持久化
           return $this->_success($this->param,'上传成功！');
        }catch (Exception $exception){
            $this->_error(['msg'=>$exception->getMessage()],$exception->getCode());
        }
    }


    /**
     * 解决当前的用户上传后或者登陆超时后
     */
    protected function ImageQueue()
    {
        $handler                    =   $this->redisHandler->handler();
        if(empty($handler) || false===$handler)
        {
            return false;
        }
        //疑问是哪个表的商品的图片呢因为这个需要当前的表的加上我们的商品序号加以存储

    }

}